﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LPLS.Commons.Enums
{
    public  enum DataTypeEnum:int
    {


        [Description("Int16")]
        int16 = 1,
        [Description("Int32")]
        int32 = 2,
        [Description("Int64")]
        int64 = 3,
        [Description("Floot")]
        Floot = 4,
        [Description("Double")]
        Double = 5,
        [Description("Bool")]
        Bool = 6,
        [Description("String")]
        String = 7,
    }




    public static class DataTypeEnumExtensions
    {
        /// <summary>
        /// 获取数据类型的描述信息
        /// </summary>
        /// <param name="collectionType"></param>
        /// <returns></returns>
        public static string GetDescription(this DataTypeEnum collectionType)
        {
            var type = collectionType.GetType();
            var memberInfo = type.GetMember(collectionType.ToString());
            if (memberInfo.Length > 0)
            {
                var attributes = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attributes.Length > 0)
                {
                    return ((DescriptionAttribute)attributes[0]).Description;
                }
            }
            return collectionType.ToString();
        }
        /// <summary>
        /// 获取所有采集方式枚举
        /// </summary>
        /// <returns></returns>
        public static List<DataTypeEnum> GetAllCollectionTypes()
        {
            return Enum.GetValues(typeof(DataTypeEnum))
                .Cast<DataTypeEnum>()
                .ToList();
        }
        /// <summary>
        /// 获取所有采集方式的描述信息列表
        /// </summary>
        /// <returns></returns>
        public static List<string> GetAllCollectionTypeDescriptions()
        {
            return GetAllCollectionTypes()
                .Select(ct => ct.GetDescription())
                .ToList();
        }

        /// <summary>
        /// 通过字符串获取对应的采集方式枚举 不区分大小写
        /// </summary>
        /// <param name="description"></param>
        /// <returns></returns>
        public static DataTypeEnum GetCollectionTypeByDescription(string description)
        {
            return GetAllCollectionTypes()
                .FirstOrDefault(ct => ct.GetDescription().ToUpper().Equals(description.ToUpper(), StringComparison.OrdinalIgnoreCase));
        }

    }
}
